package com.luobo.home.controller;

import com.luobo.home.pojo.Question;
import com.luobo.home.service.QuestionMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/questions")
@Transactional
public class QuestionController {
    private final QuestionMapper questionMapper;

    public QuestionController(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    @GetMapping("/sequential")
    public ResponseEntity<Question>  getNextQuestion(@RequestParam Integer currentBankId,@RequestParam Long currentId) {
        Question q = questionMapper.findNextQuestion(currentId,currentBankId);
        if (q == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); // 204 状态码
        }
        q.setAppearCount(q.getAppearCount() + 1);
        questionMapper.updateAppearCount(q);
        return ResponseEntity.ok(q);
    }

    @GetMapping("/random")
    public ResponseEntity<Question>  getRandomQuestion(@RequestParam Integer currentBankId,
                                                       @RequestParam(required = false) List<Long> excludedIds
                                                       ) {

        // 处理空参数：转换为空列表
        excludedIds = excludedIds == null ? Collections.emptyList() : excludedIds;
        Question q = questionMapper.findCandidates(excludedIds,currentBankId);
        if (q == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); // 204 状态码
        }
        System.out.println("q.getAppearCount() = " + q.getAppearCount());
        q.setAppearCount(q.getAppearCount() + 1);
        questionMapper.updateAppearCount(q);
        return ResponseEntity.ok(q);
//        List<Question> candidates = questionMapper.findCandidates(excludedIds);
//        if (candidates.isEmpty()) {
//            return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); // 204 状态码
//        }
//        // 权重计算逻辑保持不变,筛选逻辑有问题，暂不分析。
//        double totalWeight = 0;
//        List<Double> weights = new ArrayList<>();
//        for (Question q : candidates) {
//            double weight = q.getAppearCount() == 0 ? 1.0 :
//                    1.0 - ((double) q.getCorrectCount() / q.getAppearCount());
//            weights.add(weight);
//            totalWeight += weight;
//        }
//
//        double random = Math.random() * totalWeight;
//        double current = 0;
//        for (int i = 0; i < candidates.size(); i++) {
//            current += weights.get(i);
//            if (current >= random) {
//                Question selected = candidates.get(i);
//                selected.setAppearCount(selected.getAppearCount() + 1);
//                questionMapper.updateAppearCount(selected);
//                return ResponseEntity.ok(selected);
//            }
//        }
//        return null;
    }

    @PostMapping("/{id}/answer")
    public Map<String, Object> checkAnswer(@PathVariable Long id, @RequestBody Map<String, String> body) {
        Question q = questionMapper.findById(id);
        boolean correct = body.get("selectedOption").equalsIgnoreCase(q.getAnswer().trim());
        if (correct) {
            q.setCorrectCount(q.getCorrectCount() + 1);
            questionMapper.updateCorrectCount(q);
        } else {
            q.setIncorrectCount(q.getIncorrectCount() + 1);
            questionMapper.updateIncorrectCount(q);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("correct", correct);
        result.put("correctAnswer", q.getAnswer());
        return result;

    }
}
